package com.wanxing.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wanxing.adapter.MyAdapter;
import com.zhny.wxginger.R;
import com.util.HttpUtils;
import com.util.Info;

import vo.DTU_data;
import vo.DTU_inf;
import android.content.Context;
import android.media.Image;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DataShowFragment extends Fragment {
	private TextView title;
	private View mView;

	private TextView d_odtu;
	private TextView d_otype;
	private TextView d_ocrops;
	private TextView d_ostage;
	private TextView d_odate;
	private TextView d_otime;
	private ImageButton d_refresh;
	private int[] imageResId;
	private ListView listView;
	private SimpleAdapter adapter;
	private ArrayList<HashMap<String, Object>> list;

	private DTU_data dtu_data = new DTU_data();
	private DTU_inf dtu_inf = new DTU_inf();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.data_show, container, false);
		/*
		 * Toast.makeText(getActivity(),
		 * getActivity().getActionBar().getTitle(), Toast.LENGTH_SHORT).show();
		 */
		System.out.println("111111");

		init();

		d_refresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String dtu = getActivity().getActionBar().getTitle().toString();
				String url2 = Info.path4 + "?DTU=" + dtu;
				String url1 = Info.path3 + "?DTU=" + dtu;

				new MyAsyncTask1().execute(url1);
				new MyAsyncTask2().execute(url2);
				Toast.makeText(getActivity(), "ˢ�³ɹ���", 1).show();
			}
		});

		return mView;
	}

	private void init() {
		d_odtu = (TextView) mView.findViewById(R.id.d_dtu);
		d_otype = (TextView) mView.findViewById(R.id.d_type);
		d_ocrops = (TextView) mView.findViewById(R.id.d_crops);
		d_ostage = (TextView) mView.findViewById(R.id.d_stage);
		d_otime = (TextView) mView.findViewById(R.id.d_time);
		d_odate = (TextView) mView.findViewById(R.id.d_date);
		listView = (ListView) mView.findViewById(R.id.data_listview);
		d_refresh = (ImageButton) mView.findViewById(R.id.d_refresh);

		String dtu = getActivity().getActionBar().getTitle().toString();
		String url1 = Info.path3 + "?DTU=" + dtu;
		new MyAsyncTask1().execute(url1);

		String url2 = Info.path4 + "?DTU=" + dtu;
		new MyAsyncTask2().execute(url2);

	}

	// DTU_inf �첽����
	public class MyAsyncTask1 extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... arg0) {
			System.out.println(arg0[0] + "      DTU_inf �첽����");
			return HttpUtils.sendPostMethod(arg0[0], "utf-8");
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			if (result == null) {
				dtu_inf = new DTU_inf(null, null, null, null);
			} else {

				dtu_inf = jsonINF(result);
			}
			d_odtu.setText("��ţ�" + dtu_inf.getDTU());
			d_otype.setText(dtu_inf.getDTU_type());
			d_ocrops.setText("���" + dtu_inf.getDTU_crops());
			d_ostage.setText(dtu_inf.getDTU_stage());
		}

	}

	private DTU_inf jsonINF(String result) {

		DTU_inf dtu_inf = new DTU_inf();
		try {
			JSONArray array = new JSONArray(result);
			JSONObject jsonObject = new JSONObject();
			for (int i = 0; i < array.length(); i++) {
				jsonObject = array.getJSONObject(i);
				dtu_inf = new DTU_inf(jsonObject.getString("DTU"),
						jsonObject.getString("DTU_type"),
						jsonObject.getString("DTU_crops"),
						jsonObject.getString("DTU_stage"));
				System.out
						.println("asdasdasdasdasda!@!@!@!@!@!@!@!SDASDASDASDASD");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("lgq" + dtu_inf.toString());
		return dtu_inf;
	}

	// DTU_data �첽����
	public class MyAsyncTask2 extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... arg0) {
			System.out.println(arg0[0] + "      DTU_data �첽����");
			return HttpUtils.sendPostMethod(arg0[0], "utf-8");
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result == null) {
				dtu_data = new DTU_data(null, null, null, null, null, null,
						null, null, null, null, null);
				Toast.makeText(getActivity(), "���� ʧ��", 1).show();
			} else {

				dtu_data = jsonIDATA(result);
				list = getItem(dtu_data);
			}
			adapter = new SimpleAdapter(getActivity(), list,
					R.layout.data_show_item, new String[] { "Dimg", "Dname",
							"Ddate", "Dunit" }, new int[] {
							R.id.data_imageView1, R.id.data_name,
							R.id.data_vaule, R.id.data_unit });
			listView.setAdapter(adapter);

		}

	}

	private DTU_data jsonIDATA(String result) {

		DTU_data dtu_data = new DTU_data();
		try {
			JSONArray array = new JSONArray(result);
			JSONObject jsonObject = new JSONObject();
			for (int i = 0; i < array.length(); i++) {
				jsonObject = array.getJSONObject(i);
				dtu_data = new DTU_data(jsonObject.getString("DTU"),
						jsonObject.getString("datatime"),
						jsonObject.getString("soilhumi1"),
						jsonObject.getString("soilhumi2"),
						jsonObject.getString("soilhumi3"),
						jsonObject.getString("soiltemp"),
						jsonObject.getString("airtemp"),
						jsonObject.getString("airhumi"),
						jsonObject.getString("sundata"),
						jsonObject.getString("coo"),
						jsonObject.getString("yanfen"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("lgq" + dtu_inf.toString());
		return dtu_data;
	}

	public ArrayList<HashMap<String, Object>> getItem(DTU_data dtu_data) {
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (dtu_data.getDatetime() == null) {
			d_odate.setText("��ѯʧ��");
			d_otime.setText("");
		}
		if (dtu_data.getDatetime() != null) {
			String t = dtu_data.getDatetime().substring(0,
					dtu_data.getDatetime().length() - 2);
			String str[] = t.split(" ");
			d_odate.setText(str[0]);
			d_otime.setText(str[1]);
		}

		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.airtemp);
		map.put("Dname", "�����¶�");
		map.put("Ddate", dtu_data.getAirtemp());
		map.put("Dunit", "��");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.airhumipp);
		map.put("Dname", "����ʪ�� ");
		map.put("Ddate", dtu_data.getAirhumi());
		map.put("Dunit", "%");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.light);
		map.put("Dname", "����ǿ�� ");
		map.put("Ddate", dtu_data.getSundata());
		map.put("Dunit", "KLX");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.co);
		map.put("Dname", "������̼");
		map.put("Ddate", dtu_data.getCoo());
		map.put("Dunit", "PPM");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.soiltemp);
		map.put("Dname", "�����¶� ");
		map.put("Ddate", dtu_data.getSoiltemp());
		map.put("Dunit", "��");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.soilhumi);
		map.put("Dname", "����ʪ��1");
		map.put("Ddate", dtu_data.getSoilhumi1());
		map.put("Dunit", "%");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.soilhumi);
		map.put("Dname", "����ʪ��2");
		map.put("Ddate", dtu_data.getSoilhumi2());
		map.put("Dunit", "%");
		list.add(map);
		// map = new HashMap<String, Object>();
		// map.put("Dimg", R.drawable.soilhumi);
		// map.put("Dname", "����ʪ��3");
		// map.put("Ddate", dtu_data.getSoilhumi3());
		// map.put("Dunit", "%");
		// list.add(map);
		map = new HashMap<String, Object>();
		map.put("Dimg", R.drawable.soilyanfen);
		map.put("Dname", "�����η�");
		map.put("Ddate", dtu_data.getYanfen());
		map.put("Dunit", "mS/cm");
		list.add(map);

		System.out.println(list);
		return list;

	}

}
